import os
import sys
import easygui
import pathlib
import time
from openpyxl import Workbook
from openpyxl.styles import fills,colors,NamedStyle,Font,Side,Border,PatternFill,Alignment,Protection

root=pathlib.Path(os.path.dirname(os.path.realpath(sys.argv[0])))

bd = Border(
    left=Side(border_style="thin",
                color='000000'),
    right=Side(border_style="thin",
                color='000000'),
    top=Side(border_style="thin",
                color='000000'),
    bottom=Side(border_style="thin",
                color='000000')
                )
alignment=Alignment(horizontal='center',#水平'center', 'centerContinuous', 'justify', 'fill', 'general', 'distributed', 'left', 'right'
            vertical='center',#垂直'distributed', 'bottom', 'top', 'center', 'justify'
            text_rotation=0,#旋转角度0~180
            wrap_text=True,#文字换行
            shrink_to_fit=False,#自适应宽度，改变文字大小,上一项false
            indent=0)
        
localtime = time.localtime(time.time())
print(localtime.tm_year)
wb=Workbook()
bj=easygui.textbox("输入班级名称,每行一个班级","输入班级名称")
if bj=="":
    easygui.msgbox("请输入班级")
    exit()
bj=bj.split("\n")
for i in bj:
    if i!="" :
        if i[-1]!='班':
            s=i+"班"
        else:
            s=i
        ws=wb.create_sheet(s)
        ws.cell(1,1,"鲁甸县新街中学{year}年春季学期九年级周考{bj}成绩汇总".format(year=str(localtime.tm_year),bj=s))
        ws.merge_cells(start_row=1, end_row=1, start_column=1, end_column=14)
        ws.cell(1,1).font=Font(size=24) #字体
        ws.cell(1,1).alignment=alignment
        ws.row_dimensions[1].height = 60   #行高
        title=['学号','班级','姓名','语文','数学','英语','物理','化学','政治','历史','体育','实验','其它小科目总分','总分']
        cols=1
        for i in title:
            ws.cell(2,cols,i).border=bd
            ws.cell(2,cols).alignment=alignment
            cols=cols+1
wb.remove(wb["Sheet"])  
i=0
while(True):
    try:
        wb.save(root/"导入模板.xlsx")
    except:
        if(i>=5):
            easygui.msgbox("文件保存失败!")
            exit()
        t=easygui.exceptionbox("文件保存失败，请关闭Excel")
        
        print(t)
    else:
        easygui.msgbox("模板保存在 {}".format(str((root/"导入模板.xlsx"))),"模板保存成功")
        break
    i=i+1


